package com.anghui.demo.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.anghui.demo.domain.RmProject;
import com.anghui.demo.mapper.RmProjectMapper;
import com.anghui.demo.service.RmProjectService;
import com.anghui.demo.vo.request.ReqProjectVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class RmProjectServiceImpl extends ServiceImpl<BaseMapper<RmProject>, RmProject> implements RmProjectService {
    @Autowired
    private RmProjectMapper rmProjectMapper;
//    @Autowired
//    private Snowflake snowflake;

    @Override
    public IPage<RmProject> selectRmProjectList() {
        IPage<RmProject> page = new Page<>(1, 10);
        List<RmProject> rmProjects = rmProjectMapper.selectRmProjectList(page);
        page.setRecords(rmProjects);
        return page;
    }

    @Override
    public IPage<RmProject> selectRmProjectListPlus() {
        IPage<RmProject> page = new LambdaQueryChainWrapper<>(rmProjectMapper).page(new Page<>(1, 10));
        return page;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String insertProject(ReqProjectVo reqProjectVo) {
        RmProject rmProject = new RmProject();
        BeanUtil.copyProperties(reqProjectVo, rmProject, "updateBy", "updateTime");
//        rmProject.setProjectId(snowflake.nextId());
        int insert = rmProjectMapper.insert(rmProject);
//        throw new RuntimeException("测试异常日志记录");
        if (insert > 0) {
            return "添加成功";
        }
        return "添加失败";
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String updateProject(ReqProjectVo reqProjectVo) {
        RmProject rmProject = new RmProject();
        BeanUtil.copyProperties(reqProjectVo, rmProject, "createBy", "createTime");
        int update = rmProjectMapper.updateById(rmProject);
        if (update > 0) {
            return "更新成功";
        }
        return "更新失败";
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String deleteProjects() {
        LambdaQueryWrapper<RmProject> wrapper = new LambdaQueryWrapper<>();
        wrapper.likeRight(RmProject::getProjectName, "桌面配置器");
        int delete = rmProjectMapper.deleteProjects(wrapper);
        if (delete > 0) {
            return "删除成功";
        }
        return "删除失败";
    }
}
